var TYPE_MONITOR = 2;
var TYPE_SERVER = 1;
var TYPE_CONNECTOR = 0;

function rndIdx(max) {return Math.floor(Math.random()*(max+1))%(max+1);}
function get(i,j) {i+=game.cols*10;j+=game.rows*10;return board.children[i%game.cols+(j%game.rows)*game.cols];}

function getNeibous2(item, k) {return getNeibous(item.col, item.row, k);}
function getNeibous(i, j, k) {
    var ni = i + (k==0?1:(k==2?-1:0));
    var nj = j + (k==1?1:(k==3?-1:0));
    return get(ni+game.cols, nj+game.rows)
}


function restart(filling) {

    if (filling==1) difficulty.text="Hard"
    else if (filling==0) difficulty.text="Easy"
    else difficulty.text="Normal"
    meter.value = 0;

    //reset board
    for (var i=0; i<game.cols; i++) {
        for (var j=0; j<game.rows;j++) {
            get(i,j).removeWires();
            get(i,j).col = i;
            get(i,j).row = j;
            get(i,j).type = TYPE_CONNECTOR;
        }
    }

    //server position in the middle
    var s_i =  Math.floor(game.cols/2)-1+rndIdx(1);
    var s_j = Math.floor(game.rows/2)-1+rndIdx(1);
    get(s_i,s_j).type = TYPE_SERVER;

    var startfromitems = new Array();
    var item = get(s_i,s_j);
    startfromitems.push(item);
    var min_item_count = game.cols*game.rows/4;
    min_item_count += (game.cols*game.rows-5-min_item_count) * filling;

    //fill board
    while (startfromitems.length < min_item_count) {
        item = startfromitems[rndIdx(startfromitems.length-1)];
        while (true) {
            var dir = (rndIdx(3)*90);
            if (!getNeibous2(item, dir/90).hasWires()) {
                item.setWired(dir);
                item = getNeibous2(item, dir/90);
                item.setWired(dir+180);
                startfromitems.push(item);
            } else {
                break;
            }
        }
    }
    //mark monitors
    for (var i=0; i<game.cols; i++) {
        for (var j=0; j<game.rows;j++) {
            if (get(i,j).type == TYPE_SERVER) continue;
            var c = 0;
            for (var k=0; k<4; k++) {
                c+= get(i,j).isWired(k*90)?1:0;
            }
            if (c==1) get(i,j).type=2;
            if (c==0) get(i,j).type=22;
        }
    }
    //shuffle it

    for (var i=0; i<game.cols; i++) {
        for (var j=0; j<game.rows;j++) {
            get(i,j).wires[0].parent.rotation = rndIdx(3)*90;
        }
    }
   // refreshConnections();
}

function refreshConnectionsStep(item) {
    item.state = "On";
    for (var k=0; k<4; k++) {
        if (item.isWired(k*90) && getNeibous2(item, k).isWired(k*90+180)&& getNeibous2(item, k).state=="") {
            refreshConnectionsStep(getNeibous2(item, k));
        }
    }
}

function refreshConnections() {
    var si=0;
    var sj=0;

    for (var i=0; i<game.cols; i++) {
        for (var j=0; j<game.rows;j++) {
            if (get(i,j).type == TYPE_SERVER) {
                si=i;
                sj=j;
            }

            get(i,j).state="";
        }
    }
    refreshConnectionsStep(get(si, sj));
    //check if all houses ON
    var gameover = true;
    for (var i=0; i<game.cols; i++) {
        for (var j=0; j<game.rows;j++) {
            if (get(i,j).type == TYPE_MONITOR && get(i,j).state=="") gameover = false;
        }
    }
    return gameover;
}
